﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using JHErp.Domain;
using System.Text.RegularExpressions;

namespace JHErp
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void btnImpTaobaoOrders_Click(object sender, EventArgs e)
        {
            this.ImpTaobaoOrders();
        }

        private void btnImpAlibabaOrders_Click(object sender, EventArgs e)
        {

        }

        private void btnImpAlipayRecords_Click(object sender, EventArgs e)
        {

        }

        public void ImpTaobaoOrders()
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = "c:\\";//注意这里写路径时要用c:\\而不是c:\
            openFileDialog.Filter = "文本文件|*.*|C#文件|*.cs|所有文件|*.*";
            openFileDialog.RestoreDirectory = true;
            openFileDialog.FilterIndex = 1;

            IList<TaobaoOrder> list = new List<TaobaoOrder>();
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string fn = openFileDialog.FileName;
                FileStream fs = new FileStream(fn, FileMode.Open);
                using (StreamReader sr = new StreamReader(fs,Encoding.Default))
                {
                    string line = sr.ReadLine();
                    while (line!=null)
                    {
                        string[] field = line.Split(',');
                        if (field.Length == 45)
                        {
                            if (!field[0].Contains("订单编号"))//跳过标题
                            {
                                var ins = new TaobaoOrder()
                                {
                                    order_id = T(field[0]),//A
                                    buyer_nick= T(field[1]),//B
                                    buyer_alipay = T(field[2]),//C
                                    buyer_payable = T(field[3]),//D
                                    E = T(field[4]),//E
                                    F = T(field[5]),//F
                                    G = T(field[6]),//G
                                    H = T(field[7]),//H
                                    I = T(field[8]),//I
                                    J = T(field[9]),//J
                                    K = T(field[10]),//K
                                    L = T(field[11]),//L
                                    M = T(field[12]),//M
                                    N = T(field[13]),//N
                                    O = T(field[14]),//O
                                    P = T(field[15]),//P
                                    Q = T(field[16]),//Q
                                    R = T(field[17]),//R
                                    S = T(field[18]),//S
                                    T = T(field[19]),//T
                                    U = T(field[20]),//U
                                    V = T(field[21]),//V
                                    W = T(field[22]),//W
                                    X = T(field[23]),//X
                                    Y = T(field[24]),//Y
                                    Z = T(field[25]),//Z
                                    AA = T(field[26]),//AA
                                    AB = T(field[27]),//AB
                                    AC = T(field[28]),//AC
                                    AD = T(field[29]),//AD
                                    AE = T(field[30]),//AE
                                    AF = T(field[31]),//AF
                                    AG = T(field[32]),//AG
                                    AH = T(field[33]),//AH
                                    AI = T(field[34]),//AI
                                    AJ = T(field[35]),//AJ
                                    AK = T(field[36]),//AK
                                    AL = T(field[37]),//AL
                                    AM = T(field[38]),//AM
                                    AN = T(field[39]),//AN
                                    AO = T(field[40]),//AO
                                    AP = T(field[41]),//AP
                                    AQ = T(field[42]),//AQ
                                    AR = T(field[43]),//AR
                                    AS = T(field[44])//AS
                                };
                                list.Add(ins);
                            }
                        }

                        line = sr.ReadLine();
                    }
                }
            }

            using (var db = new ERPContext())//保存到数据库
            {
                db.TaobaoOrders.AddRange(list);
                db.SaveChanges();
            }
        }

        private void btnList_Click(object sender, EventArgs e)
        {
            new frmList().Show();
        }

        private string T(string input)
        {
            return Regex.Replace(input, "^\"'|^\"|^=\"|\"$", "");
        }
    }
}
